/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package de.hwrberlin.remo.model.impl;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

import de.hwrberlin.remo.model.AndRule;
import de.hwrberlin.remo.model.ElementState;
import de.hwrberlin.remo.model.LinksFromFacts;
import de.hwrberlin.remo.model.LinksFromRules;
import de.hwrberlin.remo.model.RemoModelPackage;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>And Rule</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class AndRuleImpl extends RuleImpl implements AndRule {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected AndRuleImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return RemoModelPackage.Literals.AND_RULE;
	}
	
	@Override
	public ElementState checkRule() {
		boolean hasUnkown = false;
		boolean hasFalse  = false;
		
		EList<LinksFromRules> linksfromrules = this.getRelatedlinkedrules();
		EList<LinksFromFacts> linksfromfacts = this.getRelatedlinkedfacts();
		
		System.out.println(linksfromrules.size());
		System.out.println(linksfromfacts.size());
		System.out.println("---------------------");
		
		if(linksfromrules.size() > 0){
		for(LinksFromRules premise : linksfromrules) {
			ElementState state = premise.getRule().getState(); 
			
			if((state == ElementState.FALSE) && (premise.getLinkstate() == ElementState.FALSE)) {
				hasFalse = true;
			}
			if((state == ElementState.TRUE) && (premise.getLinkstate() == ElementState.TRUE)) {
				hasFalse = true;
			}else if(state == ElementState.UNKNOWN) {
				hasUnkown = true;
			}
		}}
		
		if(linksfromfacts.size() > 0){
		for(LinksFromFacts premise : linksfromfacts) {
			ElementState state = premise.getFact().getState(); 
			
			if((state == ElementState.FALSE) && (premise.getLinkstate() == ElementState.FALSE)) {
				hasFalse = true;
			}
			if((state == ElementState.TRUE) && (premise.getLinkstate() == ElementState.TRUE)) {
				hasFalse = true;
			}else if(state == ElementState.UNKNOWN) {
				hasUnkown = true;
			}
		}}
		
		if(hasFalse) {
			return ElementState.FALSE;
		
		} else if(hasUnkown) {
			return ElementState.UNKNOWN;
		}
		
		return ElementState.TRUE;
	}

} //AndRuleImpl
